package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {


    @Reference
    private CheckGroupService checkGroupService;
    /**
     * 添加新的检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return checkGroupService.findPage(queryPageBean);
    }

    /**
     * 根据id查询检查组信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据id 查询检查组相关的检查项的id
     * @param id
     * @return
     */
    @RequestMapping("/findCheckItemIds")
    public Result findCheckItemIds(Integer id){

        try {
            List<Integer> list = checkGroupService.findCheckItemIds(id);
            return new Result(true,"查询检查项集合成功",list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询检查项集合失败");
        }
    }

    /**
     * 更新检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("/editCheckGroup")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result editCheckGroup(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){

        try {
            checkGroupService.updateCheckGroup(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 第一次删除检查组,可能有关联
     * @param id
     * @return
     */

    @RequestMapping("/deleteFirst")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result deleteFirst(Integer id){

        try {
            String strFlag = checkGroupService.deleteFirst(id);
            if ("N".equals(strFlag)){
                return new Result(true,"N");
            }else {
                return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 第二次删除检查组,包括关联
     * @param id
     * @return
     */
    @RequestMapping("/deleteSecond")
    public Result deleteSecond(Integer id){

        try {
            checkGroupService.deleteSecond(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查询所有的检查组
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){

        try {
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

}
